<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>房屋管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f5f5;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            overflow: hidden;
        }

        .header {
            background: #2c3e50;
            color: white;
            padding: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .header h1 {
            font-size: 24px;
        }

        .add-btn {
            background: #27ae60;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            transition: background 0.3s;
        }

        .add-btn:hover {
            background: #219a52;
        }

        .house-list {
            padding: 0;
        }

        .house-item {
            display: flex;
            align-items: center;
            padding: 15px 20px;
            border-bottom: 1px solid #eee;
            transition: background 0.3s;
        }

        .house-item:hover {
            background: #f8f9fa;
        }

        .house-item:last-child {
            border-bottom: none;
        }

        .house-info {
            flex: 1;
            display: grid;
            grid-template-columns: 2fr 2fr 1fr;
            gap: 20px;
            align-items: center;
        }

        .house-name {
            font-weight: 600;
            color: #2c3e50;
        }

        .house-location {
            color: #7f8c8d;
        }

        .house-price {
            font-weight: 600;
            color: #e74c3c;
            font-size: 16px;
        }

        .edit-btn {
            background: #3498db;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
            transition: background 0.3s;
            margin-left: 15px;
        }

        .edit-btn:hover {
            background: #2980b9;
        }

        .delete-btn {
            background: #e74c3c;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
            transition: background 0.3s;
            margin-left: 10px;
        }

        .delete-btn:hover {
            background: #c0392b;
        }

        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.5);
            z-index: 1000;
        }

        .modal-content {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: white;
            padding: 30px;
            border-radius: 8px;
            width: 400px;
            max-width: 90%;
        }

        .modal h2 {
            margin-bottom: 20px;
            color: #2c3e50;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: 500;
            color: #34495e;
        }

        .form-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }

        .form-group input:focus {
            outline: none;
            border-color: #3498db;
        }

        .modal-buttons {
            display: flex;
            gap: 10px;
            justify-content: flex-end;
            margin-top: 20px;
        }

        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }

        .btn-primary {
            background: #3498db;
            color: white;
        }

        .btn-primary:hover {
            background: #2980b9;
        }

        .btn-secondary {
            background: #95a5a6;
            color: white;
        }

        .btn-secondary:hover {
            background: #7f8c8d;
        }

        .empty-state {
            text-align: center;
            padding: 40px 20px;
            color: #7f8c8d;
        }

        .loading {
            text-align: center;
            padding: 20px;
            color: #7f8c8d;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>房屋管理系统</h1>
            <button class="add-btn" onclick="openAddModal()">+ 添加房屋</button>
        </div>
        
        <div class="house-list" id="houseList">
            <div class="loading">加载中...</div>
        </div>
    </div>

    <!-- 添加/编辑房屋模态框 -->
    <div class="modal" id="houseModal">
        <div class="modal-content">
            <h2 id="modalTitle">添加房屋</h2>
            <form id="houseForm">
                <div class="form-group">
                    <label for="houseName">房屋名称</label>
                    <input type="text" id="houseName" name="title" required>
                </div>
                <div class="form-group">
                    <label for="houseLocation">位置</label>
                    <input type="text" id="houseLocation" name="location" required>
                </div>
                <div class="form-group">
                    <label for="housePrice">价格</label>
                    <input type="number" id="housePrice" name="price" required>
                </div>
                <div class="modal-buttons">
                    <button type="button" class="btn btn-secondary" onclick="closeModal()">取消</button>
                    <button type="submit" class="btn btn-primary">确认</button>
                </div>
            </form>
        </div>
    </div>

    <script>
        // 环境配置
        const API_BASE_URL = 'http://snuc/api';
        
        let houses = [];
        let editingHouseId = null;

        // 初始化
        document.addEventListener('DOMContentLoaded', function() {
            loadHouses();
        });

        // 加载房屋列表
        async function loadHouses() {
            try {
                const response = await fetch(`${API_BASE_URL}/rooms`);
                if (response.ok) {
                    houses = await response.json();
                } else {
                    houses = [];
                }
                renderHouses();
            } catch (error) {
                console.error('加载房屋列表失败:', error);
                houses = [];
                renderHouses();
            }
        }

        // 渲染房屋列表
        function renderHouses() {
            const houseList = document.getElementById('houseList');
            
            if (houses.length === 0) {
                houseList.innerHTML = '<div class="empty-state">暂无房屋信息</div>';
                return;
            }

            houseList.innerHTML = houses.map(house => `
                <div class="house-item">
                    <div class="house-info">
                        <div class="house-name">${house.title}</div>
                        <div class="house-location">${house.location}</div>
                        <div class="house-price">¥${house.price}/月</div>
                    </div>
                    <button class="edit-btn" onclick="openEditModal(${house.id})">修改</button>
                    <button class="delete-btn" onclick="deleteHouse(${house.id})">删除</button>
                </div>
            `).join('');
        }

        // 打开添加模态框
        function openAddModal() {
            editingHouseId = null;
            document.getElementById('modalTitle').textContent = '添加房屋';
            document.getElementById('houseForm').reset();
            document.getElementById('houseModal').style.display = 'block';
        }

        // 打开编辑模态框
        function openEditModal(houseId) {
            editingHouseId = houseId;
            const house = houses.find(h => h.id === houseId);
            if (!house) return;

            document.getElementById('modalTitle').textContent = '编辑房屋';
            document.getElementById('houseName').value = house.title;
            document.getElementById('houseLocation').value = house.location;
            document.getElementById('housePrice').value = house.price;
            document.getElementById('houseModal').style.display = 'block';
        }

        // 关闭模态框
        function closeModal() {
            document.getElementById('houseModal').style.display = 'none';
            editingHouseId = null;
        }

        // 提交表单
        document.getElementById('houseForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const formData = new FormData(this);
            const houseData = {
                title: formData.get('title'),
                location: formData.get('location'),
                price: parseInt(formData.get('price'))
            };

            try {
                let response;
                if (editingHouseId) {
                    // 编辑房屋
                    response = await fetch(`${API_BASE_URL}/rooms/${editingHouseId}`, {
                        method: 'PUT',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify(houseData)
                    });
                    
                    if (response.ok) {
                        const index = houses.findIndex(h => h.id === editingHouseId);
                        if (index !== -1) {
                            houses[index] = { ...houses[index], ...houseData };
                        }
                    }
                } else {
                    // 添加房屋
                    response = await fetch(`${API_BASE_URL}/rooms`, {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify(houseData)
                    });
                    
                    if (response.ok) {
                        const newHouse = await response.json();
                        houses.push(newHouse);
                    }
                }
                
                renderHouses();
                closeModal();
            } catch (error) {
                console.error('操作失败:', error);
                closeModal();
            }
        });

        // 删除房屋
        async function deleteHouse(houseId) {
            if (!confirm('确定要删除这个房屋吗？')) {
                return;
            }

            try {
                const response = await fetch(`${API_BASE_URL}/rooms/${houseId}`, {
                    method: 'DELETE'
                });
                
                if (response.ok) {
                    houses = houses.filter(h => h.id !== houseId);
                    renderHouses();
                }
            } catch (error) {
                console.error('删除失败:', error);
            }
        }

        // 点击模态框外部关闭
        document.getElementById('houseModal').addEventListener('click', function(e) {
            if (e.target === this) {
                closeModal();
            }
        });
    </script>
</body>
</html>